package main

import (
	"fmt"
	"gorm-study/src/pkg/student/models"
	"gorm-study/src/pkg/utils/dbutil"

	"gorm.io/gorm"
)

func main() {
	dbutil.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Create(&models.Student{Name: "熊大"}).Error; err != nil {
			// 返回任何错误都会回滚事务
			return err
		}

		if err := tx.Create(&models.Student{Name: "熊二"}).Error; err != nil {
			return err
		}

		// 返回 nil 提交事务
		return nil
	})

	var students []models.Student
	dbutil.DB.Find(&students)
	for _, student := range students {
		fmt.Printf("ID: %v, Name:%v \n", student.ID, student.Name)
	}
}
